package controller

import (
	"fmt"
	"github.com/cy77cc/hioshop/models"
	"github.com/cy77cc/hioshop/utils"
	"github.com/gin-gonic/gin"
	"net/http"
)

type SearchHandler struct {
}

func (*SearchHandler) Index(ctx *gin.Context) {
	res := utils.NewResponse()
	id, exits := ctx.Get("id")
	if !exits {
		res.SetRes(1, "id不存在", nil)
		ctx.JSON(http.StatusBadRequest, res)
		return
	}

	userId := id.(uint)
	keyword := models.Keywords{}
	searchHistory := models.SearchHistory{}
	searchHistory.UserId = fmt.Sprintf("%d", userId)

	defaultKeyword, err := keyword.GetDefaultKeywords()

	if err != nil {
		res.SetRes(1, "未能找到关键词，或关键词为空", err)
		ctx.JSON(http.StatusOK, res)
		return
	}

	hotKeywordList, err := keyword.GetHotKeywordList()
	if err != nil {
		res.SetRes(1, "未能找到关键词，或关键词为空", err)
		ctx.JSON(http.StatusOK, res)
		return
	}

	historyKeywordList, err := searchHistory.GetHistoryKeywordList()
	if err != nil {
		res.SetRes(1, "未能找到关键词，或关键词为空", err)
		ctx.JSON(http.StatusOK, res)
		return
	}

	data := make(map[string]any)

	data["defaultKeyword"] = defaultKeyword
	data["historyKeywordList"] = historyKeywordList
	data["hotKeywordList"] = hotKeywordList
	res.SetRes(0, "success", data)

	ctx.JSON(http.StatusOK, res)

}
